package com.infinit.easyreader.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.infinit.easyreader.Biz.BookBiz;
import com.infinit.easyreader.R;
import com.infinit.easyreader.adpter.BooksAdapter;
import com.infinit.easyreader.entity.Book;
import com.infinit.easyreader.ui.HomeActivity;
import com.infinit.easyreader.ui.ReaderActivity;
import com.infinit.easyreader.utils.FindBookType;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 类：CollectionFragment [V 1.0.0]
 * 包含类 : com.infinit.easyreader.fragment.CollectionFragment
 * 作用：收藏的Fragment
 * Created by rzy on 2016/1/30.
 */
public class CollectionFragment extends android.support.v4.app.Fragment {

    @Bind(R.id.gvCollects)
    GridView gvCollects;
    private BookBiz bookBiz;
    private RadioGroup rgEditBar;
    private LinearLayout llFooter;
    private RadioButton rbEditCollect;
    private List<Book> books;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_collections, container, false);
        ButterKnife.bind(this, view);
        Handler handler = new Handler(new Handler.Callback() {
            @SuppressWarnings("unchecked")
            @Override
            public boolean handleMessage(Message msg) {
                switch (msg.what) {
                    case BookBiz.FIND_BOOKS_SUCCESS:
                        books = (List<Book>) msg.obj;
                        BooksAdapter adapter = new BooksAdapter(books, getActivity());
                        gvCollects.setAdapter(adapter);
                        break;
                    case BookBiz.FIND_BOOKS_FAIL:
                        Toast.makeText(getActivity(), "没有已收藏的书籍", Toast.LENGTH_SHORT).show();
                        break;
                }
                return true;
            }
        });
        bookBiz = new BookBiz(getActivity(), handler);
        bookBiz.new FindBooksTask().execute(FindBookType.COLLECTED);
        getViewOfParent();
        setListener();
        return view;
    }


    /**
     * 作用：获得Activity的编辑按钮控件
     */

    private void getViewOfParent() {
        Activity activity = getActivity();
        rgEditBar = (RadioGroup) activity.findViewById(R.id.rgEditBar);
        rbEditCollect = (RadioButton) activity.findViewById(R.id.rbEditCollect);
        llFooter = (LinearLayout) activity.findViewById(R.id.footer);
    }

    /**
     * 作用：为指定的控件设置监听器
     */
    private void setListener() {

        /**
         * 作用：为GridView的子项目设置长按监听器
         * 当长按时，下方的编辑条显示，隐藏TAB栏
         */
        gvCollects.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                //当Book已经为收藏中时,显示取消收藏
                rbEditCollect.setText("收藏");
                if (books.get(position).isFavorite()) {
                    rbEditCollect.setText("取消收藏");
                }
                llFooter.setVisibility(View.GONE);
                rgEditBar.setVisibility(View.VISIBLE);
                HomeActivity activity = (HomeActivity) getActivity();
                activity.setCurrentStatus(books.get(position), bookBiz, FindBookType.COLLECTED);
                return true;
            }
        });

        /**
         * 作用：为GridView的子项目设置点击监听器，点击打开readerActivity
         */

        gvCollects.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getActivity(), ReaderActivity.class);
                intent.putExtra(BooksFragment.FILE_PATH, books.get(position));
                startActivity(intent);
            }
        });


    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        bookBiz.closeDatabase();
        bookBiz = null;
        ButterKnife.unbind(this);
    }

    /**
     * 作用：更新数据
     */
    public void updateData() {
        bookBiz.new FindBooksTask().execute(FindBookType.COLLECTED);
    }
}
